package com.sharelords.demo.common.result;

import com.sharelords.demo.common.enums.ReturnCodeEnum;

import java.io.Serializable;

/**
 * @Description: 业务返回结果
 *
 * @Author: 千古龙少
 * @Time: 2019/11/21 0:16
 */
public class ServiceResult<T> implements Serializable {

    private static final long serialVersionUID = -8891804872071996719L;

    /**
     * 返回码
     */
    private Integer returnCode;
    /**
     * 返回消息
     */
    private String returnMsg;
    /**
     * 返回结果
     */
    private T result;

    public Integer getReturnCode() {
        return returnCode;
    }

    public void setReturnCode(Integer returnCode) {
        this.returnCode = returnCode;
    }

    public String getReturnMsg() {
        return returnMsg;
    }

    public void setReturnMsg(String returnMsg) {
        this.returnMsg = returnMsg;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    @Override
    public String toString() {
        return "ServiceResult{" +
                "returnCode=" + returnCode +
                ", returnMsg='" + returnMsg + '\'' +
                ", result=" + result +
                '}';
    }

    /**
     * 成功
     *
     * @Author: 千古龙少
     * @Time: 2019/11/21 0:17
     * @param t
     * @return com.sharelords.demo.common.result.ServiceResult<T>
     */
    public ServiceResult<T> success(T t) {
        this.returnCode = ReturnCodeEnum.SUCCESS.getReturnCode();
        this.returnMsg = ReturnCodeEnum.SUCCESS.getReturnMsg();
        this.result = t;
        return this;
    }

    /**
     * 成功
     *
     * @Author: 千古龙少
     * @Time: 2019/11/21 0:17
     * @param t
     * @param returnMsg
     * @return com.sharelords.demo.common.result.ServiceResult<T>
     */
    public ServiceResult<T> success(T t, String returnMsg) {
        this.returnCode = ReturnCodeEnum.SUCCESS.getReturnCode();
        this.returnMsg = returnMsg;
        this.result = t;
        return this;
    }

    /**
     * 失败
     *
     * @Author: 千古龙少
     * @Time: 2019/11/21 0:17
     * @param returnCodeEnum
     * @return com.sharelords.demo.common.result.ServiceResult<T>
     */
    public ServiceResult<T> fail(ReturnCodeEnum returnCodeEnum) {
        if (returnCodeEnum == null) {
            returnCodeEnum = ReturnCodeEnum.FAILED;
        }
        this.returnCode = returnCodeEnum.getReturnCode();
        this.returnMsg = returnCodeEnum.getReturnMsg();
        return this;
    }

    /**
     * 失败
     *
     * @Author: 千古龙少
     * @Time: 2019/11/21 0:17
     * @param returnCode
     * @param returnMsg
     * @return com.sharelords.demo.common.result.ServiceResult<T>
     */
    public ServiceResult<T> fail(Integer returnCode, String returnMsg) {
        if (returnCode == null) {
            returnCode = ReturnCodeEnum.FAILED.getReturnCode();
        }
        this.returnCode = returnCode;
        this.returnMsg = returnMsg;
        return this;
    }
}
